package com.pathpad.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.pathpad.dao.WebappDao;
import com.pathpad.hibernate.PPUser;
import com.pathpad.hibernate.PPUserCommonPad;

public class AddController implements Controller {

	/*
	 * private HelloManager helloManager;
	 * 
	 * public void setHelloManager(HelloManager helloManager) {
	 * this.helloManager = helloManager; }
	 */

	private WebappDao webappDao;

	@Transactional
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		PPUser ppUser = (PPUser) request.getSession().getAttribute(
				"userInfo");
		if (null == request.getParameter("webName")
				|| "".equals(request.getParameter("webName"))
				|| null == request.getParameter("webURL")
				|| "".equals(request.getParameter("webURL"))) {

			request.setAttribute("hello_2", "WebName或WebURL不能够为空！！！ ");
			return new ModelAndView("login");
		} else {
			PPUserCommonPad ppUserCommonPad = new PPUserCommonPad();
			ppUserCommonPad.setPpUser(ppUser);
			ppUserCommonPad.setWebID(request.getParameter("webURL"));
			ppUserCommonPad.setWebName(request.getParameter("webName"));
			webappDao.savePPUserCommonPadTableVaule(ppUserCommonPad);
		}
		request.setAttribute("hello_2", request.getParameter("webName")
				+ " 添加成功！ ");

		List<PPUserCommonPad> resultPPUserCommonPadList = webappDao
				.findPPUserCommonPad(ppUser.getUserId());

		if (null != resultPPUserCommonPadList
				&& resultPPUserCommonPadList.size() != 0) {
			request.setAttribute("readWebURL", resultPPUserCommonPadList.get(0)
					.getWebID().toString());
			request.setAttribute("readWebName", resultPPUserCommonPadList
					.get(0).getWebName().toString());
			request.setAttribute("userWebResultList", resultPPUserCommonPadList);
		}

		return new ModelAndView("login");
	}

	public WebappDao getWebappDao() {
		return webappDao;
	}

	public void setWebappDao(WebappDao webappDao) {
		this.webappDao = webappDao;
	}

}
